<?php

class EntryLoginForm extends CFormModel
{
	public $password;
    protected $_module;
    
	public function rules()
	{
		return array(
			array('password', 'required'),
			array('password', 'authenticate'),
		);
	}

	/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	public function authenticate($attribute,$params)
	{
        if($this->password != $this->getModule()->entryPassword)
            $this->addError('password','Incorrect password.');
	}
    
    public function getModule()
    {
        if(empty($this->_module) || ! ($this->_module instanceof AuthModule))
            throw new CException('the module must be a instance of AuthModule');
        
        return $this->_module;
    }
    
    public function setModule($module)
    {
        if( ! ($module instanceof AuthModule))
            throw new CException('the module must be a instance of AuthModule');
        
        $this->_module = $module;
    }
    
    public function login()
    {
        $this->getModule()->setEntryLogined(true);
        return true;
    }
}
